package com.hp.bon.sgw.wh.channel.groupConsistency.BizSlot;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.hp.bon.sgw.wh.channel.common.BizConsts;
import com.hp.bon.sgw.wh.channel.common.BizParam;
import com.hp.bon.sgw.wh.channel.common.BizResult;
import com.hp.bon.sgw.wh.channel.invoices.BizSlot;
import com.hp.bon.sgw.wh.channel.invoices.bizSlot.HttpClientUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;
import java.util.Map;

/**
 * @PROJECT_NAME: SGW_WH2.1_TMALL
 * @DESCRIPTION:
 * @USER: YouQi
 * @DATE: 2021/5/20 16:23
 */
public class QryReturnBalanceInfoDetailSlot implements BizSlot {
    private static final Logger _log = LoggerFactory.getLogger(QryReturnBalanceInfoDetailSlot.class);

    @Override
    public BizResult doBiz(BizParam bizParam) {
        BizResult bizResult = new BizResult();
//        String method = bizParam.getParamString(BizConsts.KEY_REQUEST_METHOD);
//        String xAppId = bizParam.getParamString(BizConsts.RQUEST_HEADER_X_APP_ID);
//        String xAppKey = bizParam.getParamString(BizConsts.RQUEST_HEADER_X_APP_KEY);
        String reqBody = bizParam.getParamString(BizConsts.KEY_REQUEST_BODY);


        // 获取URL地址
        String url = bizParam.getParamString(BizConsts.KEY_ACCOUNT_API_URL);
        //       String url = "https://0f1f3a3d-15fa-4a03-bbf1-003764d88a79.mock.pstmn.io/monthlyBill";

        String respStr = HttpClientUtils.postHttpStr(reqBody, url, bizParam);


        if ("".equals(respStr)) {
            bizResult.setCode(BizConsts.STATUS_ACOUNT_API_FAIL);
            respStr = HttpClientUtils.getErrorJsonStr(BizConsts.STATUS_ACOUNT_API_FAIL, "", "", "");
        } else {
            try {
                Map<String, Object> respStrMap = HttpClientUtils.parseJSON2Map(respStr);
                @SuppressWarnings("unchecked")
                List<Map<String, Object>> returnPlanInfo = (List<Map<String, Object>>) respStrMap.get("returnPlanDetInfo");
                if (returnPlanInfo != null && returnPlanInfo.size() > 0) {
                    for (Map<String, Object> stringObjectMap : returnPlanInfo) {
                        stringObjectMap.remove("balanceTypeFlag");
                    }
                }
                respStr = JSONObject.toJSONString(respStrMap,true);
            } catch (Exception e) {
                _log.error("Failed to process external response message", e);
                bizResult.setCode(BizConsts.STATUS_ACOUNT_API_FAIL);
                respStr = HttpClientUtils.getErrorJsonStr(BizConsts.STATUS_ACOUNT_API_FAIL, "", "", "");
                bizResult.setHttpBody(respStr);
                return bizResult;
            }

            bizResult.setCode(BizConsts.STATUS_ACOUNT_API_SUCCESS);
        }
        _log.info("Responding to external messages====:{}", respStr);
        bizResult.setHttpBody(respStr);

        return bizResult;
    }

//    public static void main(String[] args) {
//        BizParam bizParam = new BizParam();
//        String body="{\n" +
//                "\t\"operAttrStruct\": {\n" +
//                "\t\t\"staffId\": \"5001241181\",\n" +
//                "\t\t\"operOrgId\": \"110\",\n" +
//                "\t\t\"operTime\": \"20190425\",\n" +
//                "\t\t\"operPost\": \"1\",\n" +
//                "\t\t\"operServiceId\": \"1\",\n" +
//                "\t\t\"lanId\": \"8340100\"\n" +
//                "\t},\n" +
//                "\t\"svcObjectStruct\": {\n" +
//                "\t\t\"objType\": \"3\",\n" +
//                "\t\t\"objValue\": \"18972113331\",\n" +
//                "\t\t\"objAttr\": \"2\",\n" +
//                "\t\t\"dataArea\": \"0\"\n" +
//                "\t}\n" +
//                "}";
//        bizParam.setParam(BizConsts.KEY_REQUEST_BODY,body);
//        QryReturnBalanceInfoDetailSlot slot = new QryReturnBalanceInfoDetailSlot();
//        BizResult bizResult = slot.doBiz(bizParam);
//
//        System.out.println(bizResult.getHttpBody());
//
//
//    }
}
